import { http } from '@/utils/http/axios';
import { RequestEnum } from "@/enums/httpEnum";
import { BasicPageParams } from "@/api/authority/user";

/** 文件夹相关 API */

/**
 * 获取公开文件夹信息
 * @param id
 */
export const getPublicDirInfo = (id: string) => {
  return http.request({
    url: `/common/dir/${id}`,
    method: RequestEnum.GET
  })
}

/**
 * 获取文件夹信息
 * @param id
 */
export const getDirInfo = (id: string) => {
  return http.request({
    url: `/dir/${id}`,
    method: RequestEnum.GET
  })
}

/**
 * 创建文件夹
 * @param name
 * @param power
 * @param parentId
 */
export const createFolder = ({ name , power , parentId }) => {
  return http.request({
    url: `/dir/${name}/${power}/${parentId}`,
    method: RequestEnum.POST
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 修改文件夹信息
 * @param id
 * @param name
 * @param power
 */
export const updateFolder = ({ id, name, power }) => {
  return http.request({
    url: `/dir/${id}/${name}/${power}`,
    method: RequestEnum.PUT
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 删除文件夹
 * @param id
 */
export const deleteFolder = (id: string) => {
  return http.request({
    url: `/dir/${id}`,
    method: RequestEnum.DELETE
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 获取文件夹列表
 */
export const getFolderList = () => {
  return http.request({
    url: `/dir/list`,
    method: RequestEnum.GET
  })
}


/** 文件相关 API */

export const fileRename = ({ fileId, name }) => {
  return http.request({
    url: `/file/rename/${fileId}/${name}`,
    method: RequestEnum.PUT
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 文件拷贝到指定文件夹/可批量
 * @param fileIds
 * @param dirId
 */
export const fileCopyTo = ({ fileIds, dirId }) => {
  return http.request({
    url: `/file/copy/${dirId}`,
    method: RequestEnum.POST,
    data: fileIds
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 文件移动到指定文件夹/可批量
 * @param fileIds
 * @param dirId
 */
export const fileMoveTo = ({ fileIds, dirId }) => {
  return http.request({
    url: `/file/move/${dirId}`,
    method: RequestEnum.POST,
    data: fileIds
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 纳入回收站/可批量
 * @param fileIds
 */
export const delete2Recycle = (fileIds: string[]) => {
  return http.request({
    url: `/file/delete`,
    method: RequestEnum.DELETE,
    data: fileIds
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 下载文件
 * @param id
 * @param signal
 * @param callback
 */
export const downloadFile = (id: string, signal: AbortSignal, callback: Function) => {
  return http.request({
    url: `/common/download/${id}`,
    method: RequestEnum.GET,
    responseType: 'blob',
    timeout: 0,
    onDownloadProgress: e => callback(e),
    signal
  }, {
    isReturnNativeResponse: true
  })
}

/** 回收站相关 API */

/**
 * 获取回收站文件列表
 */
export const getRecycleList = () => {
  return http.request({
    url: `/file/recycle`,
    method: RequestEnum.GET
  })
}

/**
 * 回收站文件还原/可批量
 * @param fileIds
 */
export const recycleRestore = (fileIds: string[]) => {
  return http.request({
    url: `/file/recycle`,
    method: RequestEnum.POST,
    data: fileIds
  }, {
    isShowSuccessMessage: true
  })
}

/**
 * 回收站文件删除/可批量
 * @param fileIds
 */
export const recycleDelete = (fileIds: string[]) => {
  return http.request({
    url: `/file/recycle`,
    method: RequestEnum.DELETE,
    data: fileIds
  }, {
    isShowSuccessMessage: true
  })
}


/** MarkDown在线编辑器 API **/

/**
 * 获取可编辑的 markdown 文件列表
 */
export const markdownPage = (info: BasicPageParams) => {
  return http.request({
    url: `/file/markdown/page/${info.pageNumber}/${info.pageSize}`,
    method: RequestEnum.GET
  })
}

/**
 * 获取 markdown 文件的信息
 * @param fileId
 */
export const markdownInfo = (fileId: string) => {
  return http.request({
    url: `/file/markdown/${fileId}`,
    method: RequestEnum.GET
  })
}

/**
 * 获取 markdown 文件的公共信息
 * @param fileId
 */
export const markdownCommonInfo = (fileId: string) => {
  return http.request({
    url: `/common/md/preview/${fileId}`,
    method: RequestEnum.GET
  }, {
    isTransformResponse: false
  })
}

/**
 * 兼顾 markdown 文件的 新建和更新
 * @param dirId
 * @param data
 */
export const markdownUpload = (
  dirId: string,
  data: {id?: string, identifier: string, name: string, content: string}
) => {
  return http.request({
    url: `/file/markdown/upload/${dirId}`,
    method: RequestEnum.POST,
    data
  })
}
